{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad2e24dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "from selenium import webdriver\n",
    "from time import sleep\n",
    "from pprint import pprint\n",
    "from selenium.webdriver.support.select import Select\n",
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3003b49d",
   "metadata": {},
   "outputs": [],
   "source": [
    "driver = webdriver.Chrome()\n",
    "driver.maximize_window() # 窗口最大化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9b035971",
   "metadata": {},
   "outputs": [],
   "source": [
    "driver.get(r\"http://s.dianping.com/event/shenzhen?utm_source=dp_pc_group\")\n",
    "sleep(2)\n",
    "print(f\"\\n---- 1.Welcome，欢迎进入霸王餐首页： ----\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ed761f98",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 登录\n",
    "login = driver.find_element_by_css_selector(\"#u-login\")\n",
    "login.click()\n",
    "sleep(20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea6b178b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 加载更多\n",
    "driver.get(r\"http://s.dianping.com/event/shenzhen?utm_source=dp_pc_group\")\n",
    "more = driver.find_element_by_xpath(\"//a[@class='load-more show']\")\n",
    "mores = more.get_attribute(\"class\")\n",
    "try:\n",
    "    while True:\n",
    "        if more.get_attribute(\"class\") == r\"load-more show\":\n",
    "            print(f\"-- 点击加载更多成功，请稍等片刻！--\")\n",
    "            more.click()\n",
    "            sleep(1)\n",
    "        elif more.get_attribute(\"class\") == r\"load-more\":\n",
    "            print(\"\\n---- 2.Success！霸王餐信息已全部加载完毕！开始循环获取商铺信息----\\n\")\n",
    "            break\n",
    "except:\n",
    "    print(\"-- 点击加载更多失败，请检查！--\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7659cf17",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 主窗口柄和标题\n",
    "mainWindows = driver.current_window_handle\n",
    "mainTitle = driver.title"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1598bc47",
   "metadata": {},
   "outputs": [],
   "source": [
    "successList = []\n",
    "failList = []\n",
    "count = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e800dd3c",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "menus = driver.find_elements_by_css_selector(\".activity-lists-wraper li\")\n",
    "sleep(2)\n",
    "for i in menus[:]:\n",
    "    sleep(0.5)\n",
    "    count=count+1\n",
    "    url = i.find_element_by_css_selector(\"a\")\n",
    "    storeUrl = i.find_element_by_css_selector(\"a\").get_attribute(\"href\")\n",
    "    storeName = i.find_element_by_css_selector(\"h4\").text\n",
    "    storeAddr = i.find_element_by_css_selector(\"div.addi>span.addr\").text\n",
    "    print(f\"{count:<3}.商家姓名：{storeName} || 地址：{storeAddr} || 链接：{storeUrl}\")\n",
    "    \n",
    "    if (not (\"餐\" in storeName or \"饮\" in storeName or \"吃\" in storeName)) \\\n",
    "        or (\"齿科\" in storeName or \"口腔\" in storeName or \"课\" in storeName \n",
    "             or \"孕\" in storeName or \"月子\" in storeName or \"产\" in storeName) : \n",
    "        # 筛选不满足条件的退出\n",
    "        continue\n",
    "    print(\"满足条件~\")\n",
    "\n",
    "    url.click()\n",
    "\n",
    "    # 跳转新窗口\n",
    "    for window in driver.window_handles:\n",
    "        driver.switch_to.window(window)\n",
    "        if window != mainWindows:\n",
    "            break\n",
    "            #--\n",
    "    sleep(random.randint(1,3))\n",
    "    \n",
    "    try:\n",
    "        button = driver.find_element_by_css_selector(\".btn-txt.J_self_apply\")\n",
    "        sleep(0.5)\n",
    "        print(f\"新标签页面按钮为：{button.text}\")\n",
    "        if \"取消报名\" in button.text: # 筛选已报名的退出\n",
    "            print(\"已经报名过了，无需理会~\")\n",
    "            # 切回主窗口\n",
    "            driver.close()\n",
    "            driver.switch_to.window(mainWindows)\n",
    "            continue\n",
    "    except Exception as ex:\n",
    "        print(ex)\n",
    "        print(\"报名截止了，无需理会~\")\n",
    "        # 切回主窗口\n",
    "        driver.close()\n",
    "        driver.switch_to.window(mainWindows)\n",
    "        continue\n",
    "\n",
    "\n",
    "    \n",
    "    button.click()\n",
    "    sleep(1.5)\n",
    "    try:\n",
    "        # abert = driver.find_element_by_css_selector(\".pop-main\")\n",
    "        # print(f\"新标签页面弹框为：{abert.text}\")\n",
    "        # abert.click()\n",
    "        try:\n",
    "            select = driver.find_element_by_css_selector(\"select.J_branch\")\n",
    "            if select:\n",
    "                Select(select).select_by_index(1)\n",
    "                sleep(0.3)\n",
    "        except Exception as ex:\n",
    "            print(ex)\n",
    "            print(\"只有单一门店，无需理会~\")\n",
    "        driver.find_element_by_css_selector(\"#J_pop_ok\").click()\n",
    "        sleep(1)\n",
    "        abert1 = driver.find_element_by_css_selector(\".pop-main .col.tit\")\n",
    "        print(f\"报名成功页面弹框为：{abert1.text}\\n\")\n",
    "        sleep(1)\n",
    "        # 关闭弹窗\n",
    "        driver.find_element_by_css_selector(\".hd a.close\").click()\n",
    "        sleep(1.5)\n",
    "        successList.append(f\"{count}.{storeName}|{storeUrl}\")\n",
    "    except Exception as e:\n",
    "        print(e)\n",
    "        print(f\"报名异常，请人工检查连接地址：{count}.{storeName}:{storeUrl}\\n\")\n",
    "        failList.append(f\"{count}.{storeName}|{storeUrl}\")\n",
    "    finally:\n",
    "        # 关闭新标签\n",
    "        driver.close()\n",
    "        \n",
    "    # 切回主窗口\n",
    "    driver.switch_to.window(mainWindows)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "driver.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.8.6 64-bit"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6"
  },
  "metadata": {
   "interpreter": {
    "hash": "63fd5069d213b44bf678585dea6b12cceca9941eaf7f819626cde1f2670de90d"
   }
  },
  "interpreter": {
   "hash": "2f464fc6b098c045d3fa2a81389409b13ae3524050d1c22ae6b1fc19c12f7a78"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}